package com.qdlc.p2p.biz.service.activity;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.qdlc.p2p.biz.service.activity.plan.CustomActivityPlanHandler;
import com.qdlc.p2p.biz.service.activity.plan.DefaultActivityPlanHandler;
import com.qdlc.p2p.common.util.BeanUtil;

/**
 * 活动计划处理器工具类
 * @author linya
 */
public class ActivityPlanHandlers {

	private static Map<String, ActivityPlanHandler> handlerMaps = null;
	
	public static void defaultHandle(ActivityPlanParam planParam) throws ActivityPlanException{
		handle(planParam, DefaultActivityPlanHandler.class);
	}
	
	public static void customHandle(ActivityPlanParam planParam) throws ActivityPlanException{
		handle(planParam, CustomActivityPlanHandler.class);
	}
	
	public static void handle(ActivityPlanParam planParam, Class<? extends ActivityPlanHandler> clazz) throws ActivityPlanException{
		
		if(handlerMaps == null){
			synchronized (ActivityPlanHandlers.class) {
				if(handlerMaps == null){
					initHandlers();
				}
			}
		}
		
		String key = StringUtils.uncapitalize(clazz.getSimpleName());
		ActivityPlanHandler handler = handlerMaps.get(key);
		if(handler == null){
			throw new ActivityPlanException("no found ActivityPlanHandler type for:" + clazz.getName());
		}
		
		handler.doPlan(planParam);
	}
	
	private static void initHandlers(){
		Map<String, ActivityPlanHandler> maps = BeanUtil.getBeansOfType(ActivityPlanHandler.class);
		handlerMaps = new HashMap<String, ActivityPlanHandler>(maps); 
	}
	
}
